<template>
  <view class="home">
    <view class="home-container">
      <Banner :list="swiperData"></Banner>
      <SubCategory :list="menusData"></SubCategory>
      <News :list="newsData"></News>
      <Promotion :list="promotionData"></Promotion>
      <NewProduct :list="newProductData"></NewProduct>
    </view>
  </view>
</template>

<script>
  import Banner from './components/Banner'
  import SubCategory from './components/SubCategory'
  import News from './components/News'
  import Promotion from './components/Promotion'
  import NewProduct from './components/NewProduct'

  import { homeData as homeDataApi } from '@/api/home'
  const app = getApp()
  export default {
    components: {
      Banner,
      SubCategory,
      News,
      Promotion,
      NewProduct
    },
    data() {
      return {
        swiperData: {},
        menusData: {},
        newsData: {},
        promotionData: {},
        newProductData: {}
      }
    },
    onLoad() {
      this._getHomeData()
    },
    methods: {
      objToDefault(data, k = 'name') {
        const ret = {}
        for (let key in data) {
          if (data[key][k]) {
            ret[data[key][k]] = data[key]
          }
        }
        return ret
      },
      async _getHomeData() {
        const { status, data, msg } = await homeDataApi()
        if (status === this.API_STATUS_CODE.SUCCESS) {
          data.default = this.objToDefault(data.value)

          this.swiperData = data.default.swiperBg
          this.menusData = data.default.menus
          this.newsData = data.default.news
          this.promotionData = data.default.activeParty

          app._initTabBar(data.default.pageFoot)
        } else {
          uni.showToast({
            icon: 'none',
            title: '首页数据获取失败，请刷新重试',
            duration: 3000
          })
        }
      }
    }
  }
</script>

<style>
</style>
